package com.cskaoyan.controller;

import com.cskaoyan.bean.data.PageData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.service.CouponUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.*;

/**
 * @BelongsProject: project2_market_wx
 * @BelongsPackage: com.cskaoyan.controller
 * @Author: yu
 * @email: 3188480992@qq.com
 * @CreateTime: 2024-06-15  12:21
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/wx/coupon")
public class WxCouponController {

    @Autowired
    CouponUserService couponUserService;

    @Autowired
    CouponService couponService;


    /**
     * 根据coupon的状态来选择coupon_ser表中的当前用户的优惠券
     *
     * @param status
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/mylist")
    public BaseRespVo mylist(Integer status, Integer page, Integer limit) {

        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            // 通过用户Id进行表查询用户所有status状态下的优惠券
            PageData pageData = couponUserService.findCouponListById(status, page, limit, userId);

            return BaseRespVo.ok(pageData);
        } else {
            return BaseRespVo.unAuthc();
        }
    }

    @PostMapping("/receive")
    public BaseRespVo recevice(@RequestBody Map<String, Integer> mapParameter) {
        Integer couponId = mapParameter.get("couponId");
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            // 将领取的coupon添加到用户账户中
            Integer status = couponUserService.receviceNewCoupon(couponId, userId);
            if (status.equals(0)) {
                return BaseRespVo.ok(null);
            } else {
                return BaseRespVo.fail("优惠券已经领过了", 740);
            }

        } else {
            return BaseRespVo.unAuthc();
        }
    }

    @GetMapping("/list")
    public BaseRespVo list(Integer page, Integer limit) {

        Subject subject = SecurityUtils.getSubject();
        // 进行登入校验
        if (subject.isAuthenticated()) {

            PageData pageData = couponService.getList(page, limit);

            return BaseRespVo.ok(pageData);
        } else {
            return BaseRespVo.unAuthc();
        }
    }

    @GetMapping("/selectlist")
    public BaseRespVo selectList(Integer cartId, Integer grouponRulesId) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            // 返回当前用户所有可以使用的优惠券
            PageData pageData = couponUserService.getSelectList(cartId, grouponRulesId, userId);

            return BaseRespVo.ok(pageData);

        } else {
            return BaseRespVo.unAuthc();
        }
    }

    @PostMapping("/exchange")
    public BaseRespVo exchange(@RequestBody Map<String, String> map) {
        String code = map.get("code");
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
                // 兑换优惠券到用户账户当中
                Integer status = couponUserService.addExchangeCoupon(code, userId);

                if (status == 3) {
                    return BaseRespVo.fail("兑换码不正确", 742);
                } else if (status == 1) {
                    return BaseRespVo.ok(null);
                } else if (status == 0) {
                    return BaseRespVo.fail("不要重复领取哦", 742);
                } else if (status == 2) {
                    return BaseRespVo.fail("有些券已经拥有", 742);
                }
                return BaseRespVo.ok(null);

            } else {
                return BaseRespVo.unAuthc();
            }
        }
    }
